/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Event;
import entity.Invitation;
import entity.Notification;
import entity.Notification_;
import entity.User;
import entity.Warning;
import entity.WarningType;
import entity.WeatherAdvice;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author andrea
 */
@Named
@RequestScoped



public class NotificationManager {
    
    @PersistenceContext
    private EntityManager em;
    
    public NotificationManager(){}
    
    /**
     * Return the list of notification not seen
     * @param mail
     * @return
     */   
     public List<Notification> findAllUnseen(String mail) {
         
        CriteriaBuilder cb= em.getCriteriaBuilder();
        CriteriaQuery<Notification> cq = cb.createQuery(Notification.class);
        
        Root<Notification> root = cq.from(Notification.class);
        
        cq.select(cq.from(Notification.class));
        cq.where(cb.and(cb.equal(root.get(Notification_.receiver), mail),cb.isFalse(root.get(Notification_.seen))));
                
        return em.createQuery(cq).getResultList();
    }
    
    
    
    /**
     *
     * @param event
     */
    public void createInvitation(Event event){
       
       
              
       Map<String,User> participants = this.removeOwnerfromParticipants(event);
        
       for(User x : participants.values()){
           this.createInvitation(event, x);
        }
    }
    
    private void createInvitation(Event event,User participant){
        java.sql.Date today = this.getActualDate(); 
        Invitation n = new Invitation();
             n.setReceiver(participant);
             n.setSeen(false);
             n.setAbout(event);
             n.setSendDate(today);
             em.persist(n);
    }
    
    
    /**
     *
     * @param event
     * @param type
     */
    public void createWarninForAllParticipants(Event event,WarningType type){

       Map<String,User> participants = this.removeOwnerfromParticipants(event);
       for(User x : participants.values()){
           this.createWarning(event, type, x);
        }
    }
    
        /**
     *
     * @param event
     * @param type
     */
    public void createWarninForOwner(Event event,WarningType type){
        this.createWarning(event, type, event.getOwner());
    }
    
    /**
     *
     * @param event
     * @param bestDate
     */
    public void createWeatherAdvice(Event event,java.sql.Date bestDate){
        WeatherAdvice weatherAdvice = new WeatherAdvice();
        weatherAdvice.setAbout(event);
        weatherAdvice.setBestDate(bestDate);
        weatherAdvice.setReceiver(event.getOwner());
        weatherAdvice.setSendDate(this.getActualDate());
        em.persist(weatherAdvice);
    }
//---------------------------------------------------------------------------------//    
        /**
     *
     * @param event
     * @param type
     * @param receiver
     */
    private void createWarning(Event event,WarningType type,User receiver)  {
        Warning warning = new Warning();
        warning.setAbout(event);
        warning.setReceiver(receiver);
        warning.setSeen(false);
        warning.setSendDate(this.getActualDate());
        warning.setType(type);
        em.persist(warning);
    }
    
    private java.sql.Date getActualDate(){
        return   new java.sql.Date((new java.util.Date()).getTime());
    }
    
    private Map <String, User> removeOwnerfromParticipants(Event event){      
        Map<String,User> participants;
        participants  = event.getParticipants();
        participants.remove(event.getOwner().getEmail());
        return participants;
    }
    
    
}
